﻿using System.Linq;
using System.Threading;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        private FlowersDBEntities db = new FlowersDBEntities();
        private const int pageSize = 5;

        //
        // GET: /Home/

        public ActionResult Index(int pageNum = 0)
        {
            Thread.Sleep(300);

            int itemsCount = db.Flowers.Count();
            ViewData["PageSize"] = pageSize;
            ViewData["PageNum"] = pageNum;
            ViewData["ItemsCount"] = itemsCount;
            var flowers = (from flower in db.Flowers orderby flower.Name select flower).
                Skip(pageNum * pageSize).Take(pageSize).ToList();

            if (!Request.IsAjaxRequest())
            {
                return View(flowers);
            }
            else
            {
                return PartialView("IndexPage", flowers);
            }
        }

        public ActionResult Details(int id)
        {
            var flowerDetails = (from flower in db.Flowers
                                 where flower.FlowerId == id
                                 select flower).First();
            return View(flowerDetails);
        }
    }
}
